package com.fauks.gmall.manager.controller;

import com.fauks.gmall.utils.RedisUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

@RestController
public class RedissonController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RedissonClient redissonClient;


    @GetMapping("redisson")
    public String testRedisson() {
        Jedis jedis = redisUtils.getJedis();
        RLock lock = redissonClient.getLock("lock");
        lock.lock();
        try {
            String v = jedis.get("k");
            if (StringUtils.isEmpty(v)) {
                v = "0";
            }
            System.out.println("【redis压力测试】==>" + v);
            jedis.set("k", (Integer.parseInt(v) + 1) + "");
        } finally {
            lock.unlock();
            jedis.close();
        }
        return "success";
    }
}
